#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QSoundEffect>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(320, 588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("老帮主带你翻金币");
    connect(ui->actionQuit, &QAction::triggered, [=](){
        this->close();
    });

    QSoundEffect * startSound = new QSoundEffect;
    startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5-startBtn->width()*0.5, this->height() * 0.7);

    chooseScence = new ChooseLevelScene;
    connect(startBtn, &MyPushButton::clicked, [=](){
        startSound->play();//开始音效
        startBtn->zoom1();//向下跳跃
        startBtn->zoom2();//向上跳跃
        QTimer::singleShot(500, this, [=](){
            this->hide();
            chooseScence->setGeometry(this->geometry());
            chooseScence->show();
        });
    });

    //监听选择场景的返回按钮
    connect(chooseScence, &ChooseLevelScene::chooseSceneBack,[=](){
        this->setGeometry(chooseScence->geometry());
        this->show();
    });



}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    painter.drawPixmap(10, 30, pix.width(), pix.height(), pix);
}
